package com.cw.jeeyt.dao.impl.facade;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.cw.jeeyt.common.utils.StringUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeProjectDao;
import com.cw.jeeyt.dao.mapper.facade.FacadeProjectMapper;
import com.cw.jeeyt.dao.model.facade.FacadeProjectDo;
import com.cw.jeeyt.dao.model.facade.FacadeProjectExample;
import com.cw.jeeyt.dao.model.facade.FacadeProjectKeyDo;
import com.cw.jeeyt.dao.query.facade.FacadeProjectQuery;
import com.cw.lang.common.utils.SequenceTool;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 项目信息数据库操作接口实现
 *
 * @author chenw
 * @date 2019-10-31 22:35:52
 */
@Repository
public class FacadeProjectDaoImpl extends AbstractBaseDaoImpl<FacadeProjectDo, FacadeProjectMapper>
    implements FacadeProjectDao {
    /**
     * 分页查询
     *
     * @param facadeProject 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<FacadeProjectDo> selectPage(FacadeProjectQuery facadeProject, PageParam pageParam) {
        FacadeProjectExample example = buildExample(facadeProject);
        return PageHelper.<FacadeProjectDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询项目信息
     *
     * @param id
     * @return
     */
    @Override
    public FacadeProjectDo selectByKey(Long id) {
        FacadeProjectKeyDo facadeProjectKeyDo = new FacadeProjectKeyDo();
        facadeProjectKeyDo.setId(id);
        return getMapper().selectByPrimaryKey(facadeProjectKeyDo);
    }

    /**
     * 根据key删除项目信息
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(Long id) {
        FacadeProjectKeyDo facadeProjectKeyDo = new FacadeProjectKeyDo();
        facadeProjectKeyDo.setId(id);
        return getMapper().deleteByPrimaryKey(facadeProjectKeyDo);
    }

    /**
     * 查询项目信息
     *
     * @param facadeProject 条件
     * @return List<FacadeProjectDo>
     */
    @Override
    public List<FacadeProjectDo> selectByExample(FacadeProjectQuery facadeProject) {
        return getMapper().selectByExample(buildExample(facadeProject));
    }

    /**
     * 新增项目信息
     *
     * @param facadeProject 条件
     * @return int>
     */
    @Override
    public Long insertBySelective(FacadeProjectDo facadeProject) {
        if (facadeProject == null) {
            return -1L;
        }
        Long id = SequenceTool.nextLongId();
        facadeProject.setId(id);
        getMapper().insertSelective(facadeProject);
        return id;
    }

    /**
     * 修改项目信息
     *
     * @param facadeProject
     * @return
     */
    @Override
    public int updateBySelective(FacadeProjectDo facadeProject) {
        if (facadeProject == null) {
            return -1;
        }
        return getMapper().updateByPrimaryKeySelective(facadeProject);
    }

    /**
     * 构建项目信息Example信息
     *
     * @param facadeProject
     * @return
     */
    public FacadeProjectExample buildExample(FacadeProjectQuery facadeProject) {
        FacadeProjectExample example = new FacadeProjectExample();
        FacadeProjectExample.Criteria criteria = example.createCriteria();
        if (facadeProject != null) {
            //添加查询条件
            if (null != facadeProject.getId()) {
                criteria.andIdEqualTo(facadeProject.getId());
            }
            if (StringUtil.isNotEmpty(facadeProject.getProjectCode())) {
                criteria.andProjectCodeEqualTo(facadeProject.getProjectCode());
            }
            if (StringUtil.isNotEmpty(facadeProject.getProjectName())) {
                criteria.andProjectNameEqualTo(facadeProject.getProjectName());
            }
            if (StringUtil.isNotEmpty(facadeProject.getProjectComment())) {
                criteria.andProjectCommentEqualTo(facadeProject.getProjectComment());
            }
            if (null != facadeProject.getCreateBy()) {
                criteria.andCreateByEqualTo(facadeProject.getCreateBy());
            }
            if (null != facadeProject.getCreateDateBegin()) {
                criteria.andCreateDateGreaterThanOrEqualTo(facadeProject.getCreateDateBegin());
            }
            if (null != facadeProject.getCreateDateEnd()) {
                criteria.andCreateDateLessThanOrEqualTo(facadeProject.getCreateDateEnd());
            }
            if (null != facadeProject.getUpdateBy()) {
                criteria.andUpdateByEqualTo(facadeProject.getUpdateBy());
            }
            if (null != facadeProject.getUpdateDateBegin()) {
                criteria.andUpdateDateGreaterThanOrEqualTo(facadeProject.getUpdateDateBegin());
            }
            if (null != facadeProject.getUpdateDateEnd()) {
                criteria.andUpdateDateLessThanOrEqualTo(facadeProject.getUpdateDateEnd());
            }
        }
        return example;
    }
}
